package com.zheng.api.server.service.base.pay;

import com.zheng.common.constant.PayConstant;
import com.zheng.pay.dao.model.*;
import com.zheng.pay.rpc.api.PayMchInfoService;
import com.zheng.pay.rpc.api.PayMchNotifyService;
import com.zheng.pay.rpc.api.PayPayChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: lvb
 * @date: 17/9/9
 * @description:
 */
@Service
public class BaseCommonService {

    @Autowired
    private PayMchInfoService payMchInfoService;

    @Autowired
    private PayPayChannelService payPayChannelService;

    @Autowired
    private PayMchNotifyService payMchNotifyService;

    @Cacheable(value="zheng-api-server-service-ehcache",key="'baseSelectMchInfo_'+#mchid")
    public PayMchInfo baseSelectMchInfo(String mchId) {
        PayMchInfoExample example = new PayMchInfoExample();
        PayMchInfoExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        return payMchInfoService.selectFirstByExample(example);
    }

    @Cacheable(value="zheng-api-server-service-ehcache",key="'baseSelectPayChannel_'+#mchid+'_'+#channelid")
    public PayPayChannel baseSelectPayChannel(String mchId, String channelId) {
        PayPayChannelExample example = new PayPayChannelExample();
        PayPayChannelExample.Criteria criteria = example.createCriteria();
        criteria.andChannelidEqualTo(channelId);
        criteria.andMchidEqualTo(mchId);
        return payPayChannelService.selectFirstByExample(example);
    }

    public PayMchNotify baseSelectMchNotify(String orderId) {
        PayMchNotifyExample example = new PayMchNotifyExample();
        PayMchNotifyExample.Criteria criteria = example.createCriteria();
        criteria.andOrderidEqualTo(orderId);
        return payMchNotifyService.selectFirstByExample(example);
    }

    public int baseInsertMchNotify(String orderId, String mchId, String mchOrderNo, String orderType, String notifyUrl) {
        PayMchNotify mchNotify = new PayMchNotify();
        mchNotify.setOrderid(orderId);
        mchNotify.setMchid(mchId);
        mchNotify.setMchorderno(mchOrderNo);
        mchNotify.setOrdertype(orderType);
        mchNotify.setNotifyurl(notifyUrl);
        return payMchNotifyService.insertSelective(mchNotify);
    }

    public int baseUpdateMchNotifySuccess(String orderId, String result, byte notifyCount) {
        PayMchNotify mchNotify = new PayMchNotify();
        mchNotify.setStatus(PayConstant.MCH_NOTIFY_STATUS_SUCCESS);
        mchNotify.setResult(result);
        mchNotify.setNotifycount(notifyCount);
        mchNotify.setLastnotifytime(new Date());
        PayMchNotifyExample example = new PayMchNotifyExample();
        PayMchNotifyExample.Criteria criteria = example.createCriteria();
        criteria.andOrderidEqualTo(orderId);
        List values = new LinkedList<>();
        values.add(PayConstant.MCH_NOTIFY_STATUS_NOTIFYING);
        values.add(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        criteria.andStatusIn(values);
        return payMchNotifyService.updateByExampleSelective(mchNotify, example);
    }

    public int baseUpdateMchNotifyFail(String orderId, String result, byte notifyCount) {
        PayMchNotify mchNotify = new PayMchNotify();
        mchNotify.setStatus(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        mchNotify.setResult(result);
        mchNotify.setNotifycount(notifyCount);
        mchNotify.setLastnotifytime(new Date());
        PayMchNotifyExample example = new PayMchNotifyExample();
        PayMchNotifyExample.Criteria criteria = example.createCriteria();
        criteria.andOrderidEqualTo(orderId);
        List values = new LinkedList<>();
        values.add(PayConstant.MCH_NOTIFY_STATUS_NOTIFYING);
        values.add(PayConstant.MCH_NOTIFY_STATUS_FAIL);
        return payMchNotifyService.updateByExampleSelective(mchNotify, example);
    }

}
